#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json


##
# 站点调查问卷
#
class Questionnaire:
    linkMan = ''
    linkPhone = ''
    pageCount = ''
    colorStyle = ''
    otherColorStyle = ''
    layoutStyle = ''
    otherLayoutStyle = ''
    fontType = ''
    otherFontType = ''
    functions = ''
    otherFunctions = ''
    createdAt = ''

    DATA_COLUMN = ["link_man", "link_phone", "page_count", "color_style", "other_color_style", "layout_style",
                   "other_layout_style", "font_type",
                   "other_font_type", "functions", "other_functions", "created_at"]
    TABLE_NAME = 'questionnaire'

    def __init__(self):
        self.linkMan = '',
        self.linkPhone = '',
        self.pageCount = '',
        self.colorStyle = '',
        self.otherColorStyle = '',
        self.layoutStyle = '',
        self.otherLayoutStyle = '',
        self.fontType = '',
        self.otherFontType = '',
        self.functions = '',
        self.otherFunctions = ''

    def myQuestionnaire(self, linkMan, linkPhone, pageCount, colorStyle, otherColorStyle, layoutStyle, otherLayoutStyle, fontType,
                        otherFontType, functions, otherFunctions):
        if isinstance(linkMan, int):
            _linkMan = int(linkMan)
        elif isinstance(linkMan, list):
            _linkMan = json.dumps(linkMan, ensure_ascii=False)
        else:
            _linkMan = str(linkMan)
        self.linkMan = _linkMan

        if isinstance(linkPhone, int):
            _linkPhone = int(linkPhone)
        elif isinstance(linkPhone, list):
            _linkPhone = json.dumps(linkPhone, ensure_ascii=False)
        else:
            _linkPhone = str(linkPhone)
        self.linkPhone = _linkPhone

        if isinstance(pageCount, int):
            _pageCount = int(pageCount)
        elif isinstance(pageCount, list):
            _pageCount = json.dumps(pageCount, ensure_ascii=False)
        else:
            _pageCount = str(pageCount)
        self.pageCount = _pageCount

        if isinstance(colorStyle, int):
            _colorStyle = int(colorStyle)
        elif isinstance(colorStyle, list):
            _colorStyle = json.dumps(colorStyle, ensure_ascii=False)
        else:
            _colorStyle = str(colorStyle)
        self.colorStyle = _colorStyle

        if isinstance(otherColorStyle, int):
            _otherColorStyle = int(otherColorStyle)
        elif isinstance(otherColorStyle, list):
            _otherColorStyle = json.dumps(otherColorStyle, ensure_ascii=False)
        else:
            _otherColorStyle = str(otherColorStyle)
        self.otherColorStyle = _otherColorStyle

        if isinstance(layoutStyle, int):
            _layoutStyle = int(layoutStyle)
        elif isinstance(layoutStyle, list):
            _layoutStyle = json.dumps(layoutStyle, ensure_ascii=False)
        else:
            _layoutStyle = str(layoutStyle)
        self.layoutStyle = _layoutStyle

        if isinstance(otherLayoutStyle, int):
            _otherLayoutStyle = int(otherLayoutStyle)
        elif isinstance(otherLayoutStyle, list):
            _otherLayoutStyle = json.dumps(otherLayoutStyle, ensure_ascii=False)
        else:
            _otherLayoutStyle = str(otherLayoutStyle)
        self.otherLayoutStyle = _otherLayoutStyle

        if isinstance(fontType, int):
            _fontType = int(fontType)
        elif isinstance(fontType, list):
            _fontType = json.dumps(fontType, ensure_ascii=False)
        else:
            _fontType = str(fontType)
        self.fontType = _fontType

        if isinstance(otherFontType, int):
            _otherFontType = int(otherFontType)
        elif isinstance(otherFontType, list):
            _otherFontType = json.dumps(otherFontType, ensure_ascii=False)
        else:
            _otherFontType = str(otherFontType)
        self.otherFontType = _otherFontType

        if isinstance(functions, int):
            _functions = int(functions)
        elif isinstance(functions, list):
            _functions = json.dumps(functions, ensure_ascii=False)
        else:
            _functions = str(functions)
        self.functions = _functions

        if isinstance(otherColorStyle, int):
            _otherColorStyle = int(otherColorStyle)
        elif isinstance(otherColorStyle, list):
            _otherColorStyle = json.dumps(otherColorStyle, ensure_ascii=False)
        else:
            _otherColorStyle = str(otherColorStyle)
        self.otherFunctions = otherFunctions

    @staticmethod
    def split_data_clum(with_id):
        ary = []
        if with_id:
            ary.append("id")
        [ary.append(item) for item in Questionnaire.DATA_COLUMN]
        return "`" + "`,`".join(ary) + "`"

    @staticmethod
    def split_params(params):
        g_param = []
        if params.linkMan is not None and len(params.linkMan) > 0:
            g_param.append('`link_man`=' + "\'" + params.linkMan + "\'")
        if params.linkPhone is not None and len(params.linkPhone) > 0:
            g_param.append('`link_phone`=' + "\'" + params.linkPhone + "\'")
        if params.pageCount is not None and len(params.pageCount) > 0:
            g_param.append('`page_count`=' + "\'" + params.pageCount + "\'")
        if params.colorStyle is not None and len(params.colorStyle) > 0:
            g_param.append('`color_style`=' + "\'" + params.colorStyle + "\'")
        if params.otherColorStyle is not None and len(params.otherColorStyle) > 0:
            g_param.append('`other_color_style`=' + "\'" + params.otherColorStyle + "\'")
        if params.layoutStyle is not None and len(params.layoutStyle) > 0:
            g_param.append('`layout_style`=' + "\'" + params.layoutStyle + "\'")
        if params.otherLayoutStyle is not None and len(params.otherLayoutStyle) > 0:
            g_param.append('`other_layout_style`=' + "\'" + params.otherLayoutStyle + "\'")
        if params.fontType is not None and len(params.fontType) > 0:
            g_param.append('`font_type`=' + "\'" + params.fontType + "\'")
        if params.otherFontType is not None and len(params.otherFontType) > 0:
            g_param.append('`other_font_type`=' + "\'" + params.otherFontType + "\'")
        if params.functions is not None and len(params.functions) > 0:
            g_param.append('`functions`=' + "\'" + params.functions + "\'")
        if params.otherFunctions is not None and len(params.otherFunctions) > 0:
            g_param.append('`other_functions`=' + "\'" + params.otherFunctions + "\'")
        if params.createdAt is not None and len(params.createdAt) > 0:
            g_param.append('`created_at`=' + "\'" + params.createdAt + "\'")
        return ', '.join(str(i) for i in g_param)

    @staticmethod
    def to_string(with_flg):
        g_param = []
        if with_flg:
            g_param.append("`Questionnaire`.`linkMan`")
            g_param.append("`Questionnaire`.`linkPhone`")
            g_param.append("`Questionnaire`.`pageCount`")
            g_param.append("`Questionnaire`.`colorStyle`")
            g_param.append("`Questionnaire`.`otherColorStyle`")
            g_param.append("`Questionnaire`.`layoutStyle`")
            g_param.append("`Questionnaire`.`otherLayoutStyle`")
            g_param.append("`Questionnaire`.`fontType`")
            g_param.append("`Questionnaire`.`otherFontType`")
            g_param.append("`Questionnaire`.`functions`")
            g_param.append("`Questionnaire`.`otherFunctions`")
            g_param.append("`Questionnaire`.`createdAt`")
        else:
            g_param.append("`linkMan`")
            g_param.append("`linkPhone`")
            g_param.append("`pageCount`")
            g_param.append("`colorStyle`")
            g_param.append("`otherColorStyle`")
            g_param.append("`layoutStyle`")
            g_param.append("`otherLayoutStyle`")
            g_param.append("`fontType`")
            g_param.append("`otherFontType`")
            g_param.append("`functions`")
            g_param.append("`otherFunctions`")
            g_param.append("`createdAt`")
            converted_columns = [convert_to_snake_case(column) for column in g_param]
        return ', '.join(str(i) for i in converted_columns)

    @staticmethod
    def to_string_noId():
        g_param = []
        g_param.append("`Questionnaire`.`linkMan`")
        g_param.append("`Questionnaire`.`linkPhone`")
        g_param.append("`Questionnaire`.`pageCount`")
        g_param.append("`Questionnaire`.`colorStyle`")
        g_param.append("`Questionnaire`.`otherColorStyle`")
        g_param.append("`Questionnaire`.`layoutStyle`")
        g_param.append("`Questionnaire`.`otherLayoutStyle`")
        g_param.append("`Questionnaire`.`fontType`")
        g_param.append("`Questionnaire`.`otherFontType`")
        g_param.append("`Questionnaire`.`functions`")
        g_param.append("`Questionnaire`.`otherFunctions`")
        g_param.append("`Questionnaire`.`createdAt`")
        converted_columns = [convert_to_snake_case(column) for column in g_param]
        return ', '.join(str(i) for i in converted_columns)

    @staticmethod
    def chg_data_2_dict(with_id, obj):
        convert = {}
        ary = []
        if with_id:
            ary.append("id")
        [ary.append(item) for item in Questionnaire.DATA_COLUMN]
        # for clu_index in range(0, len(ary)):
        #     if ary[clu_index] == "id":
        #         setattr(convert, ary[clu_index], int(obj[clu_index]))
        #     else:
        #         setattr(convert, ary[clu_index], str(obj[clu_index]).replace("\t", ""))
        for clu_index in range(len(ary)):
            column_name = ary[clu_index]
            value = obj[clu_index]
            if column_name == "id":
                convert[column_name] = int(value)
            else:
                convert[column_name] = str(value).replace("\t", "")
        return convert

    @staticmethod
    def chg_dict_2_data(with_id, obj):
        if with_id:
            data = [
                str(obj.linkMan),
                str(obj.linkPhone),
                str(obj.pageCount),
                str(obj.colorStyle),
                str(obj.colorStyle),
                str(obj.otherColorStyle),
                str(obj.layoutStyle),
                str(obj.otherLayoutStyle),
                str(obj.fontType),
                str(obj.otherFontType),
                str(obj.functions),
                str(obj.otherFunctions),
            ]
        else:
            data = [
                str(obj.linkMan),
                str(obj.linkPhone),
                str(obj.pageCount),
                str(obj.colorStyle),
                str(obj.otherColorStyle),
                str(obj.layoutStyle),
                str(obj.otherLayoutStyle),
                str(obj.fontType),
                str(obj.otherFontType),
                str(obj.functions),
                str(obj.otherFunctions),
            ]
        return "'" + "','".join(data) + "'"


@staticmethod
def convert_to_snake_case(name):
    import re
    # Insert underscores between camel case words
    name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name)
    # Convert to lower case
    name = name.lower()
    return name
